package com.heima.behavior.service.impl;

import com.heima.behavior.config.CacheService;
import com.heima.behavior.service.ArticleInfoService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.model.behavior.dtos.ArticleInfoDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
@Slf4j
public class ArticleInfoServiceImpl implements ArticleInfoService {
    @Autowired
    CacheService cacheService;

    /**
     * app文章展示关注/收藏/点赞/不喜欢
     * 数据回显
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        //参数校验
        if (dto == null || dto.getArticleId() == null || dto.getAuthorId() == null || dto.getAuthorApUserId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取登录人信息
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser == null) {
            return ResponseResult.okResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        boolean isfollow = false;
        boolean islike = false;
        boolean isunlike = false;
        boolean iscollection = false;
        //是否关注
        Double score1 = cacheService.zScore(UserRelationConstants.FOLLOW_LIST + apUser.getId(), dto.getAuthorApUserId().toString());
        if (score1 != null) {
            isfollow = true;
        }
        //是否喜欢
        Double score2 = cacheService.zScore(UserRelationConstants.LIKE_LIST + apUser.getId(), dto.getArticleId().toString());
        if (score2 != null) {
            islike = true;
        }
        //是否不喜欢
        Double score3 = cacheService.zScore(UserRelationConstants.UNLIKE_LIST + apUser.getId(), dto.getArticleId().toString());
        if (score3 != null) {
            isunlike = true;
        }
        //是否收藏
        Double score4 = cacheService.zScore(UserRelationConstants.COLLECTION_LIST + apUser.getId(), dto.getArticleId().toString());
        if (score4 != null) {
            iscollection = true;
        }
        //封装返回
        HashMap<String, Object> map = new HashMap<>();
        map.put("isfollow", isfollow);
        map.put("islike", islike);
        map.put("isunlike", isunlike);
        map.put("iscollection", iscollection);

        return ResponseResult.okResult(map);
    }
}
